package ioperation;
import book.*;

import java.util.Scanner;

public class AddOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("新增图书");
        //1.判满
        int currnUsize = bookList.getUseSize();
        if(currnUsize == bookList.getBook().length) {
            System.out.println("书架满了...不能放了！");
            return;
        }

        //输入书的属性，并创建一本书
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你新增的书名");
        String name = scanner.nextLine();
        System.out.println("请输入你新增书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入你新增书的价格");
        int price = scanner.nextInt();
        System.out.println("请输入你新增的类型");
        String type = scanner.nextLine();

        Book newbook = new Book(name,author,price,type);

//        判断有没有这本书
        for (int i = 0; i < currnUsize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("已经有这本书了...不能插入！！！ ");
                return;
            }

//          放书
            bookList.setBookList(currnUsize,book);
            bookList.setUseSize(currnUsize + 1);

        }
    }
}
